<template>
  <div id="user">
    <el-form :model="ruleForm2" status-icon :rules="rules2" ref="ruleForm2" label-width="100px" class="demo-ruleForm">
      <el-form-item label="原始密码" prop="oldPass">
        <el-input type="password" v-model="ruleForm2.oldPass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="修改密码" prop="pass">
        <el-input type="password" v-model="ruleForm2.pass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm2')">提交</el-button>
        <el-button @click="resetForm('ruleForm2')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
  export default {
    data() {
      let validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else if(!/^[\w\W]{6,18}$/.test(value)){
          callback(new Error('密码只能是6~18位的数字、大小写字符、下划线'));
        } else {
          if (this.ruleForm2.checkPass !== '') {
            this.$refs.ruleForm2.validateField('checkPass');
          }
          callback();
        }
      };
      let validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm2.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      let validateOldPass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入旧密码'));
        } else {
          callback();
        }
      };
      return {
        ruleForm2: {
          pass: '',
          checkPass: '',
          oldPass:''
        },
        rules2: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          oldPass:[
            {validator:validateOldPass,trigger:'blur'}
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.axios.post('index?action=chang_pws',{
              old_password:this.ruleForm2.oldPass,
              new_password:this.ruleForm2.pass,
              re_password:this.ruleForm2.checkPass,
              token: localStorage['token']
            }).then((res)=>{
              if (res.data.code === -2 || res.data.code === -3 || res.data.code === -4) {
                // 跳转到登录
                localStorage['token'] = '';
                this.$router.replace({
                  path: '/login'
                });
                return false
              }
              if(res.data.code === 1){
                this.ElementUIMsg({
                  message:res.data.msg,
                  showClose: true,
                  duration:2000,
                  type:"success"
                });
                // this.ruleForm2.oldPass = '';
                // this.ruleForm2.pass = '';
                // this.ruleForm2.checkPass='';
                this.resetForm('ruleForm2');
                return false

              }
              this.ElementUIMsg({
                message:res.data.msg,
                showClose: true,
                duration:2000,
                type:"error"
              });
            }).catch((e)=>{

            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>
<style scoped>
  #user{
    width: 500px;
    margin: 50px auto;
    background-color: #fff;
    padding: 20px 30px 20px 0;
    border-radius: 10px;
    box-shadow: 4px 8px 10px #e8e8e8;
  }
</style>
